package Logic;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Model.Approval;
import Model.CommitteeRole;

/**
 * Servlet implementation class AppointmentSearch_BLL
 */
@WebServlet("/AppointmentSearch_BLL")
public class AppointmentSearch_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AppointmentSearch_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String staff = request.getParameter("staff");
		String appointment = request.getParameter("appointment");
		
		AuditLogDB.insertLog("search approved appointment", (String) request
				.getSession().getAttribute("name"));

		if (staff.equals("blank") && appointment.equals("blank")) {
			response.sendRedirect("staffapp_search.jsp?error=1");
		} else if (staff.equals("blank") && !appointment.equals("blank")) {
			// search by appointment
			String md5 = "";
			boolean check = false;
			ArrayList<Approval> app = Database.StaffAppointmentDB
					.getAllApprovedAppointments("");
			for (int i = 0; i < app.size(); i++) {
				if (app.get(i).getAppointmentID().equals(appointment)) {
					check = true;
					md5 = md5.concat(app.get(i).getMd5() + ";");
				}
			}
			if (check) {
				request.setAttribute("id", md5);
				request.getRequestDispatcher("staffapp_searchresult.jsp")
						.forward(request, response);
			} else {
				response.sendRedirect("staffapp_searchresult.jsp?error=1");
			}

		} else if (!staff.equals("blank") && appointment.equals("blank")) {
			// search by staff
			String md5 = "";
			boolean check = false;
			ArrayList<Approval> app = Database.StaffAppointmentDB
					.getAllApprovedAppointments("");

			for (int i = 0; i < app.size(); i++) {
				if (app.get(i).getName()
						.equals(Database.StaffDB.getStaffNamebyId(staff))) {
					check = true;
					md5 = md5.concat(app.get(i).getMd5() + ";");
				}
			}

			if (check) {
				request.setAttribute("id", md5);
				request.getRequestDispatcher("staffapp_searchresult.jsp")
						.forward(request, response);
			} else {
				response.sendRedirect("staffapp_searchresult.jsp?error=1");
			}
		} else {
			// search by both
			String md5 = "";
			boolean check = false;
			ArrayList<Approval> app = Database.StaffAppointmentDB
					.getAllApprovedAppointments("");

			for (int i = 0; i < app.size(); i++) {
				if (app.get(i).getAppointmentID().equals(appointment)) {
					if (app.get(i).getName()
							.equals(Database.StaffDB.getStaffNamebyId(staff))) {
						check = true;
						md5 = md5.concat(app.get(i).getMd5() + ";");
					}
				}
			}

			if (check) {
				request.setAttribute("id", md5);
				request.getRequestDispatcher("staffapp_searchresult.jsp")
						.forward(request, response);
			} else {
				response.sendRedirect("staffapp_searchresult.jsp?error=1");
			}
		}
	}
}
